package homeword.Add_and_sub;

import java.util.NoSuchElementException;

public class Repository {
    /**
     *
     * 公共库，是存放他们的共同的仓库
     */

    private int total = 0;

    synchronized void add() throws InterruptedException {
        //获取当前线程的名字
        String name = Thread.currentThread().getName();

        while (total >= 3) {
            wait();
        }
        this.total++;
        System.out.println(name + "线程,数字加1后：" + this.total);
        notifyAll();
    }

    synchronized void sub() throws InterruptedException {
        String name = Thread.currentThread().getName();
        while (total < 1) {
            wait();
        }
        this.total--;
        System.out.println(name + "线程,数字减1后：" + this.total);
        notifyAll();
    }
}

